package leetcode.code2008;

import java.util.Arrays;

import leetcode.IDebug;


//执行用时：117 ms, 在所有 Java 提交中击败了7.41%的用户
//
//内存消耗：64 MB, 在所有 Java 提交中击败了15.27%的用户
//
//通过测试用例：84 / 84
public class Solution2 implements IDebug {

	final int START = 1;
	final int END = 0;

	public long maxTaxiEarnings(int n, int[][] rides) {
		int len = rides.length << 1;
		int[][] stats = new int[len][4];
		int p = 0;
		for (int[] ride : rides) {
			int start = ride[0], end = ride[1], tip = ride[2];
			stats[p][0] = start;
			stats[p++][1] = START;
			stats[p][0] = end;
			stats[p][1] = END;
			stats[p][2] = end - start + tip;
			stats[p++][3] = start;
		}
		Arrays.sort(stats, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
		long ans = 0;
		long[] dp = new long[n + 1];
		for (int i = 0; i < len; i++) {
			int[] cur = stats[i];
			int pos = cur[0], type = cur[1], tip = cur[2], start = cur[3];
//			if (type == START) {
//				dp[pos] = ans;
//			} else {
				ans = Math.max(ans, dp[start] + tip);
				dp[pos] = ans;
//			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
